package com.doitdroid;

import android.app.ListActivity;
import android.content.ContentUris;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class TaskListActivity extends ListActivity {
	
	private static final String[] PROJECTION = new String[] { TaskListInfo.TaskList._ID, TaskListInfo.TaskList.TITLE, };
	
	private final int MENU_ADD=1, MENU_EDIT=2, MENU_DELETE=3;
	private final int GROUP_DEFAULT=0, GROUP_DEL=1;

	private boolean isDeleting = false;
	private void setIsDeleting(boolean isDeleting) {
		this.isDeleting = isDeleting;
	}
	private boolean getIsDeleting() {
		return isDeleting;
	}
	
	private boolean isEditing = false;
	private void setIsEditing(boolean isEditing) {
		this.isEditing = isEditing;
	}
	private boolean getIsEditing() {
		return isEditing;
	}
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

       // If no data was given in the intent (because we were started
        // as a MAIN activity), then use our default content provider.
        Intent intent = getIntent();
        if (intent.getData() == null) {
            intent.setData(TaskListInfo.TaskList.CONTENT_URI);
        }
        
        Cursor cursor = managedQuery(intent.getData(), PROJECTION, null, null, TaskListInfo.TaskList.DEFAULT_SORT_ORDER);

        // Used to map notes entries from the database to views
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.tasklistitemtemplate, cursor,
                new String[] { TaskListInfo.TaskList.TITLE }, new int[] { R.id.listItemTemplateText });
        setListAdapter(adapter);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(GROUP_DEFAULT, MENU_ADD, 0, "Add");
        menu.add(GROUP_DEFAULT, MENU_EDIT, 0, "Edit" );
	    menu.add(GROUP_DEL, MENU_DELETE, 0, "Delete");
	    return super. onCreateOptionsMenu(menu);
    }
    
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
	    
	    
	    return super.onPrepareOptionsMenu(menu) ;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
	    switch(item. getItemId()) {
	    case MENU_ADD:
	    	this.CreateTaskItem();
	    	return true;
	    case MENU_EDIT:
	    	this.StartTaskItemEdition();
	    	return true;
	    case MENU_DELETE:
	    	this.StartTaskItemDeletion();
	    	return true;
	    }
	    return super.onOptionsItemSelected(item) ;
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
    	if(this.getIsDeleting())
    		this.DeleteTaskItem(id);
    	else if (this.getIsEditing())
    		this.EditTaskItem(id);
    	else
    	    this.OpenTaskItem(id);
    }

	private void StartTaskItemDeletion() {
		this.setIsDeleting(true);
		this.setIsEditing(false);
		
		Toast.makeText(this, "Select Item for delete", Toast.LENGTH_SHORT).show();
	}

	private void StartTaskItemEdition() {
		this.setIsDeleting(false);
		this.setIsEditing(true);
		
		Toast.makeText(this, "Select Item for edit", Toast.LENGTH_SHORT).show();
	}
	
	private void DeleteTaskItem(long id) {
		Uri noteUri = ContentUris.withAppendedId(getIntent().getData(), id);
        getContentResolver().delete(noteUri, null, null);
        
        this.setIsDeleting(false);
		this.setIsEditing(false);
	}
	
	private void EditTaskItem(long id) {
		Uri uri = ContentUris.withAppendedId(getIntent().getData(), id);
        
        String action = getIntent().getAction();
        if (Intent.ACTION_PICK.equals(action) || Intent.ACTION_GET_CONTENT.equals(action)) {
            // The caller is waiting for us to return a note selected by
            // the user.  The have clicked on one, so return it now.
            setResult(RESULT_OK, new Intent().setData(uri));
        } else {
            // Launch activity to view/edit the currently selected item
            startActivity(new Intent(Intent.ACTION_EDIT, uri));
        }
        
        this.setIsDeleting(false);
		this.setIsEditing(false);
	}
	
	private void OpenTaskItem(long id) {
		Uri uri = ContentUris.withAppendedId(TaskInfo.Task.CONTENT_URI, id);
		startActivity(new Intent(Intent.ACTION_VIEW, uri));
	}
	
	private void CreateTaskItem() {
		startActivity(new Intent(Intent.ACTION_INSERT, getIntent().getData()));
	}
}